package com.photoeditor.demo.ui.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.common.base.ComponentContext;
import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;

/**
 * Created by Administrator on 2017/6/16 0016.
 */

public class ChangeableRectButton extends View {

    private String mText = "Get";
    private String mText2 = "";
    private Paint mBgPaint;
    private Paint mTextPaint;
    private int mBgColor;
    private int mTextColor;
    private float mTextSize = DeviceUtils.sp2pxF(ComponentContext.getContext(), 15);
    private float mDp4 = DeviceUtils.dip2px(ComponentContext.getContext(), 5);
    private float mDp2 = DeviceUtils.dip2px(ComponentContext.getContext(), 2);
    private float mDp10 = DeviceUtils.dip2px(ComponentContext.getContext(), 10);
    private float mWidth;
    private float mHeight;
    private float mOffset;
    private float mRadius;
    private RectF mRectF;
    private int mBaseLineY;
    private Bitmap mBitmap;
    private float mBitmapX;
    private float mTextX;
    private int mType = 1;
    public static final int TYPE_VIDEO_AD = 1;
    public static final int TYPE_GP = 2;

    public ChangeableRectButton(Context context) {
        this(context, null);
        init(context, null);
    }

    public ChangeableRectButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ChangeableRectButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        getAttrs(context, attrs);
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
//        mTextPaint.setTypeface(FontUtil.CUSTOM_FONT);

        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgPaint.setStyle(Paint.Style.FILL);
        mBgPaint.setColor(mBgColor);
        mRectF = new RectF();
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_ad_video_white);
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ChangeableRectButton);
        mBgColor = typedArray.getColor(R.styleable.ChangeableRectButton_changeBtnBgColor, Color.WHITE);
        mTextColor = typedArray.getColor(R.styleable.ChangeableRectButton_changeBtnTextColor, Color.BLACK);
    }

    public void setGPType() {
        mType = TYPE_GP;
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_gp);
        mText = getResources().getString(R.string.download_on);
        mText2 = getResources().getString(R.string.google_play);
        invalidate();
    }

    public void setVideoADType(boolean withGPIcon) {
        mType = TYPE_VIDEO_AD;
        if (withGPIcon) {
            mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_gp);
            mText = getResources().getString(R.string.download_on);
            mText2 = getResources().getString(R.string.google_play);
        } else {
            mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_ad_video_white);
            mText = getResources().getString(R.string.store_get);
            mText2 = "";
        }
        invalidate();
    }

    public int getType() {
        return mType;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        mOffset = 0;
        mRadius = h / 2;

        //计算文字高度
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mBaseLineY = (int) (h / 2 - fontMetrics.top / 2 - fontMetrics.bottom / 2);
        mBitmapX = mWidth / 2 - (Math.max(mTextPaint.measureText(mText), mTextPaint.measureText(mText2)) + mDp10 + mBitmap.getWidth()) / 2;
        mTextX = mBitmapX + mDp10 + mBitmap.getWidth();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        float textWidth = Math.max(mTextPaint.measureText(mText), mTextPaint.measureText(mText2));
        setMeasuredDimension((int) (textWidth + mDp10 * 3 + mBitmap.getWidth()), DeviceUtils.dip2px(ComponentContext.getContext(), 56));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mRectF.set(mOffset, 0, mWidth, mHeight);
        if (mOffset > 0) {
            canvas.drawRoundRect(mRectF, mRadius, mRadius, mBgPaint);
        } else {
            canvas.drawRoundRect(mRectF, mRadius, mRadius, mBgPaint);
            canvas.drawBitmap(mBitmap, mBitmapX, mHeight / 2 - mBitmap.getHeight() / 2, mBgPaint);
            if (!TextUtils.isEmpty(mText2)) {
                canvas.drawText(mText, mTextX + mTextPaint.measureText(mText) / 2, mBaseLineY / 2 + mDp4, mTextPaint);
                canvas.drawText(mText2, mTextX + mTextPaint.measureText(mText2) / 2, mHeight / 2 + mBaseLineY / 2 - mDp2, mTextPaint);
            } else {
                canvas.drawText(mText, mTextX + mTextPaint.measureText(mText) / 3, mBaseLineY, mTextPaint);
            }
        }
    }


    public void setOffset() {
        mOffset = mWidth - mHeight;
        invalidate();
    }

    public void startAnim(final ChangeBtnListener listener) {
        ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
        animator.setDuration(400);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mOffset = (mWidth - mHeight) * value;
                invalidate();
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                listener.onAnimEnd();
            }
        });
        animator.setStartDelay(500);
        animator.start();
    }


    public interface ChangeBtnListener {
        void onAnimEnd();
    }
}
